package com.ruoyi.web.pojo.request.insect;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.net.url.UrlBuilder;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.CommonUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.warning.WarningInsect;
import com.ruoyi.web.utils.BasePageQuery;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class InsectListRequest extends BasePageQuery{

    @ApiModelProperty("昆虫类型 1 一类害虫 2 二类害虫 3 其他")
    private Integer insectType;

    @ApiModelProperty("昆虫名称")
    private String insectName;

    @ApiModelProperty("是否启用 0 启用 1 停用")
    @TableField("is_enable")
    private String isEnable;

    @ApiModelProperty(value = "数据开始时间,格式 yyyy-MM-dd")
    private String startTime;

    @ApiModelProperty(value = "数据结束时间,格式 yyyy-MM-dd")
    private String endTime;

    @ApiModelProperty(value = "数据开始时间,格式 yyyy-MM-dd")
    private String initStartTime;

    @ApiModelProperty(value = "数据结束时间,格式 yyyy-MM-dd")
    private String initEndTime;

    @ApiModelProperty("设备ID,不传查询账号下所有, 多个设备id用逗号隔开")
    private String deviceId;

    @ApiModelProperty("设备ID,非必须")
    private List<String> deviceIds;

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
        this.deviceIds = Arrays.asList(deviceId.split(","));
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
        this.initStartTime = startTime;
    }

    public void setEndTime(String endTime) {
        if(endTime != null){
            this.endTime = CommonUtils.computeTime(endTime,1);
            this.initEndTime = CommonUtils.computeTime(endTime,1);
        }
    }

    /**
     * @desc  构建查询条件
     * @author fwx 2024/2/1 14:25
     **/
    public void bulidWrapper(QueryWrapper<WarningInsect> query){


        //昆虫名称
        if(StringUtils.isNotBlank(this.getInsectName())){
            query.lambda().like(WarningInsect::getInsectName,this.getInsectName());
        }

        //是否启用
        if(StringUtils.isNotBlank(this.getIsEnable())){
            query.lambda().eq(WarningInsect::getIsEnable,this.getIsEnable());
        }

        //昆虫类型
        if(this.getInsectType() != null){
            query.lambda().eq(WarningInsect::getInsectType,this.getInsectType());
        }

        query.lambda().orderByAsc(WarningInsect::getIsEnable);
    }

    /**
     * @desc  修改时间为上一年
     * @author fwx 2024/3/7 11:23
     **/
    public void changeTimePreviousYear(){
        this.startTime = CommonUtils.computeTimeYear(initStartTime, -1);
        this.endTime = CommonUtils.computeTimeYear(initEndTime, -1);
    }

    /**
     * @desc  修改时间为 查询所有数据的时间
     * @author fwx 2024/3/7 11:23
     **/
    public void changeTimeBeforeYear(){
        this.startTime = CommonUtils.computeTimeYear(initStartTime, -10);

        String previous = CommonUtils.computeTimeYear(initEndTime, -1);
        this.endTime = DateUtils.getYearEndDate(previous.split(",")[0]);
    }

    /**
     * @desc 修改时间为当前年的开始和结束时间
     * @author fwx 2024/3/7 11:36
     **/
    public void changeTimeCurrYearAll(){
        this.startTime = DateUtils.getYearBeginDate(initStartTime.split(",")[0]);
        this.endTime = DateUtils.getYearEndDate(initEndTime.split(",")[0]);
    }

    /**
     * @desc  修改时间为前一年的开始和结束时间
     * @author fwx 2024/3/7 11:45
     **/
    public void changeTimePreviousYearAll(){
        this.startTime = CommonUtils.computeTimeYear(initStartTime, -1);
        this.startTime = DateUtils.getYearBeginDate(startTime.split(",")[0]);
        this.endTime = DateUtils.getYearEndDate(startTime.split(",")[0]);
    }

    public UrlBuilder getQuery(){
        UrlBuilder builder = UrlBuilder.of();
        if (this.getInsectName() != null) builder.addQuery("insectName",this.getInsectName());

        if (CollUtil.isNotEmpty(deviceIds)){
            builder.addQuery("deviceId",this.getDeviceIds().stream().collect(Collectors.joining(",")));
        }

        if (this.getStartTime() != null) builder.addQuery("startTime",this.getStartTime());
        if (this.getEndTime() != null) builder.addQuery("endTime",this.getEndTime());


        return builder;
    }
}


